package com.xiaowu.netmusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaowu.netmusic.domain.dto.CommentAddDTO;
import com.xiaowu.netmusic.domain.vo.CommentVO;
import com.xiaowu.netmusic.service.CommentService;
import com.xiaowu.netmusic.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comment")
@AllArgsConstructor
@Tag(name = "评论管理", description = "提供评论的添加、查询、删除等功能接口")
public class CommentController {

    private final CommentService commentService;

    @Operation(summary = "添加评论", description = "用户对歌曲、歌单等目标添加评论")
    @PostMapping("/add")
    public Result<?> add(@RequestBody CommentAddDTO dto) {
        return commentService.addComment(dto) ? Result.success() : Result.error("添加失败");
    }

    @Operation(summary = "获取评论列表", description = "分页获取目标类型（如歌曲、歌单）的评论列表")
    @GetMapping("/list")
    public Result<?> list(
            @RequestParam String targetType,
            @RequestParam Long targetId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestHeader(value = "userId", required = false) Long currentUserId
    ) {
        IPage<CommentVO> result = commentService.getCommentPage(targetType, targetId, page, size, currentUserId);
        return Result.success(result);
    }

    @Operation(summary = "删除评论", description = "用户或管理员删除指定评论")
    @DeleteMapping("/delete")
    public Result<?> delete(
            @RequestParam Long commentId,
            @RequestParam Long userId,
            @RequestParam(defaultValue = "false") boolean isAdmin) {
        return commentService.deleteComment(commentId, userId, isAdmin)
                ? Result.success()
                : Result.error("删除失败");
    }
}
